package com.pansoft.kline.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.pansoft.kline.model.IndexSelectEvent;
import com.pansoft.openplanet.R;

import org.greenrobot.eventbus.EventBus;

import static com.pansoft.klinelib.chart.KLineView.CHART_MAIN_INDEX;
import static com.pansoft.klinelib.chart.KLineView.CHART_SUB_INDEX;


/**
 *图标的ma，boll等指标选择弹窗
 * @author will
 */
public class ChartIndexPopupWindow {

    private Context context;
    private View view;
    private View decorView;

    public ChartIndexPopupWindow(Context context, View view, View decorView) {
        this.context = context;
        this.view = view;
        this.decorView = decorView;
        initView();
    }

    private void initView() {
        PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        View view = LayoutInflater.from(context).inflate(R.layout.chart_index_pop,  null, false);

        popupWindow.setContentView(view);
        setUpSelected(view);
        setOnSelectListener(popupWindow, view);
        popupWindow.showAsDropDown(decorView);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                decorView.findViewById(R.id.iv_full_right_top_triangle).setVisibility(View.INVISIBLE);
                decorView.findViewById(R.id.iv_full_right_bottom_triangle).setVisibility(View.VISIBLE);
            }
        });
    }

    private void setUpSelected(View popView) {
        if ("MA".equals(CHART_MAIN_INDEX)) {
            ((TextView)popView.findViewById(R.id.tv_ma_index))
                    .setTextColor(context.getResources().getColor(R.color.chart_accent_text_color));
        } else {
            ((TextView)popView.findViewById(R.id.tv_boll_index))
                    .setTextColor(context.getResources().getColor(R.color.chart_accent_text_color));
        }

        if ("MACD".equals(CHART_SUB_INDEX)) {
            ((TextView)popView.findViewById(R.id.tv_macd_index))
                    .setTextColor(context.getResources().getColor(R.color.chart_accent_text_color));
        } else {
            ((TextView)popView.findViewById(R.id.tv_kdj_index))
                    .setTextColor(context.getResources().getColor(R.color.chart_accent_text_color));
        }
    }

    private void setOnSelectListener(final PopupWindow popupWindow, View popView) {
        ((TextView)popView.findViewById(R.id.tv_ma_index)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new IndexSelectEvent("MA"));
                popupWindow.dismiss();
            }
        });
        ((TextView)popView.findViewById(R.id.tv_macd_index)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new IndexSelectEvent("MACD"));
                popupWindow.dismiss();
            }
        });
        ((TextView)popView.findViewById(R.id.tv_kdj_index)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new IndexSelectEvent("KDJ"));
                popupWindow.dismiss();
            }
        });
        ((TextView)popView.findViewById(R.id.tv_boll_index)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new IndexSelectEvent("BOLL"));
                popupWindow.dismiss();
            }
        });
    }
}
